package com.my.test.thread.lock;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantReadWriteLock;



public class RWLock {

    static ReentrantReadWriteLock wrl = new ReentrantReadWriteLock();

    static Lock read = wrl.readLock();

    static Lock write = wrl.writeLock();

    static Map<String,Object> cacheMap = new HashMap<>();

    // 线程B/C/D
    public static final  Object get (String key){
        read.lock();  // 获得锁阻塞

        try {
            return cacheMap.get(key);
        } finally {
            read.unlock();
        }

    }



    // 线程A
    public static final void put (String key,Object obj){
        write.lock();  // 获得血锁
        try {
            cacheMap.put(key,obj);
        } finally {
            write.unlock();
        }

    }

    /**
     * 读多写少的场景
     * @param args
     */
    public static void main(String[] args) {

        // 读读共享  读写互斥  写写互斥



    }
}
